package com.yuchen.utils.security;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;

import java.io.UnsupportedEncodingException;

/**
 * 加密和解密
 */
public class Base64Utils {

    public static String encodeBase64(String origin){
        return Base64.encodeBase64URLSafeString(origin.getBytes());
    }

    public static String encodeBase64(String origin,String salt){
        origin+=salt;
        return encodeBase64(origin);
    }

    public  static String decodeBase64(String base64){
        return new String(Base64.decodeBase64(base64.getBytes()));
    }

    public static String decodeBase64(String base64,String salt){
        String temp = decodeBase64(base64);
        return StringUtils.substring(temp,0,(temp.length()-salt.length()));
    }

    public static void main(String[] args) {
        String encode = Base64Utils.encodeBase64("1@buyer","qt1");
        System.out.println(encode);

        String  decode = Base64Utils.decodeBase64("MUBidXllcnF0","qt1");
        System.out.println(decode);
    }
}
